package com.java46.struts2.FoodType.action;

import com.alibaba.fastjson.JSON;
import com.java46.struts2.Food.service.FoodService;
import com.java46.struts2.FoodType.service.FoodTypeService;
import com.java46.struts2.base.action.BaseAction;
import com.java46.struts2.base.entity.GenericResult;
import com.java46.struts2.base.entity.PageEntity;
import com.java46.struts2.base.entity.TDishestypeEntity;
import com.java46.struts2.base.entity.vo.TypeFoods;
import com.java46.struts2.base.exception.BusinessException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;


import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName FoodTypeAction
 * @Description 菜品类型的Action路径操作
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodTypeAction extends BaseAction {
    private FoodTypeService foodTypeService = new FoodTypeService();
    private FoodService foodService = new FoodService();
    private String food;
    public String getFood(){
        return  food;
    }

    //设置菜品的get/set方法
    public void  setFood(){this.food = food;}

    /**
     *
      * 添加菜品类型
      *
      * @author Tigger Wu
      * @date 2018/7/17 9:41
      * @param
      * @return
      */
//  @Action(value = "addType")
//    public  String  addFoodType(){
//        //暂时写死
//        foodTypeService.addType("aaaa");
//        GenericResult result = new GenericResult();
//        result.setSuccess(true);
//        result.setData("yes");
//        return "json";
//    }
    /*
     *  查询全部菜品类型
     *
     * @author Tigger Wu
     * @date 2018/7/17 9:42
     * @param
     * @return
     */
     Logger loger = LogManager.getLogger(FoodTypeAction.class);
     @Action(value = "queryTypes")
        public String queryTypes(){
        List<TypeFoods>  dlist = (List<TypeFoods>)foodTypeService.queryAll();

         for(int i = 0 ;i<dlist.size();i++){
             TypeFoods foodType = dlist.get(i);
             Integer[] arr  = foodService.queryFoodsByTypeId(foodType.getdTypeId());
             TypeFoods typeFoods = dlist.get(i);
             System.out.println(arr.length);
             typeFoods.setdArr(arr);
         }
        setSuccesResult(dlist);

        return "json";
    }
/*
 * 删除菜品类型根据id
 *
 * @author Tigger Wu
 * @date 2018/7/17 10:23
 * @param id
 * @return
 */
    @Action(value = "removeType")
    public  String removeType(int id){
        foodTypeService.removeType(id);
        return "json";
    }
/*
 *  修改菜品类型
 *
 * @author Tigger Wu
 * @date 2018/7/17 11:08
 * @param
 * @return
 */
@Action(value = "updateType")
    public String updateType( TDishestypeEntity type){
        foodTypeService.updateType(type);
        return "json";
    }

    @Action(value = "queryFoodType")
    /**
     *@Author xiao
     *@Description //分页查询订单可加关键字
     *@Date 9:28 2018/7/18
     *@Param []
     *@return void
     */
    public String queryFoodType(){
        String pagesize="";
        //获取页条数
        try {
            pagesize = request.getParameter("pagesize");
        }catch (Exception e){
            pagesize="";
        }
        String keyword="";
        //获取关键字
        try {
            keyword = request.getParameter("keyword");
        }catch (Exception e){
            keyword="";
        }
        String pageno="";
        //获取页码
        try {
            pageno = request.getParameter("pageno");
        }catch (Exception e){
            pageno="1";
        }
        PageEntity pageEntity = FoodTypeService.queryFoodType(pageno, pagesize, keyword);
        setSuccesResult(pageEntity);
        return "json";
    }

    /**
     *@Author xiao
     *@Description //根据菜品类型编号删除菜品类型
     *@Date 11:38 2018/7/18
     *@Param []
     *@return void
     */
    @Action(value = "deleteType")
    public void deleteType(){
        String typeid=null;
        //获取前台订单编号
        try {
            typeid = request.getParameter("typeid");
        }catch (Exception e){
            throw new BusinessException("没有接收到菜品类型数据");
        }
        FoodTypeService.deleteType(typeid);
    }

    @Action(value = "addType")
    /**
     * @Author xiaobo
     * @Description //添加菜品类型
     * @Date 16:56 2018/7/22 0022
     * @Param []
     * @return void
     */
    public void addType(){
        String typename=null;
        try {
            typename = request.getParameter("typename");
        }catch (Exception e){
            throw new BusinessException("没有接收到菜品类型数据");
        }
        FoodTypeService.addType(typename);
    }


}
